package com.secneo.appshare.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.secneo.appshare.AppShareApplication;
import com.secneo.appshare.R;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.ActivityUtil;
import com.secneo.appshare.common.AppSharePreference;
import com.secneo.appshare.database.AppShareDatabaseHelper;
import com.secneo.mp.api.database.DatabaseHelper;

public class MemberSettingActivity extends Activity{
	
	private LinearLayout memberSettingModifyHeaderLinearLayout,memberSettingAccountLinearLayout;
	private LinearLayout memberSettingHonorLinearLayout, memberSettingLogoutLinearLayout, memberSettingRegisterSinaLinearLayout;
	private CheckBox pushCheckBox;
	

	public void logoutBang()
	{
		final AlertDialog dialog = new AlertDialog.Builder(this).create();
		LayoutInflater inflater = LayoutInflater.from(this);
		try{
			View backupExpandHeader = inflater.inflate(R.layout.sec_opt_scan_stop_dialog, null);
			TextView popTitle = (TextView) backupExpandHeader.findViewById(R.id.popDialogTitle);
			popTitle.setText(this.getResources().getString(R.string.manage_bang_logout));
			TextView popInfo = (TextView) backupExpandHeader.findViewById(R.id.popDialogInfo);
			popInfo.setText(this.getResources().getString(R.string.manage_bang_logout_alert));
			dialog.show();
			dialog.setContentView(backupExpandHeader);
	
			Button okButton = (Button) backupExpandHeader.findViewById(R.id.ButtonOK);
			okButton.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					DestroyBangAccountThread destroyBangThread = new DestroyBangAccountThread(MemberSettingActivity.this);
					destroyBangThread.start();
					dialog.dismiss();
					String shortName=LocalStore.getLocaleStoreInstance(MemberSettingActivity.this).getNickName();
					ActivityUtil.delShortcut(MemberSettingActivity.this, shortName);
					LocalStore.getLocaleStoreInstance(MemberSettingActivity.this).setUserIconShortCutCreated(false);
					AppShareDatabaseHelper adh = new AppShareDatabaseHelper(MemberSettingActivity.this);
					adh.cleanAppFileTable();
					adh.close();
					AppSharePreference.setAppallStatus(MemberSettingActivity.this, false);
					AppSharePreference.setAppMdShaStatus(MemberSettingActivity.this, false);
					Intent intent=new Intent();
					intent.setClass(MemberSettingActivity.this, LoginChooseMethodActivity.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
					MemberSettingActivity.this.startActivity(intent);
					finish();
				}
			});
	
			Button cancelButton = (Button) backupExpandHeader.findViewById(R.id.ButtonCancel);
			cancelButton.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					dialog.dismiss();
				}
			});
		} catch (Exception ex){
			ex.printStackTrace() ;
		}
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		this.setContentView(R.layout.member_setting);
		AppShareApplication.getInstance().addActivity(this);
		memberSettingModifyHeaderLinearLayout=(LinearLayout)findViewById(R.id.MemberSettingModifyHeaderLinearLayout);
		memberSettingAccountLinearLayout=(LinearLayout)findViewById(R.id.MemberSettingAccountLinearLayout);
		memberSettingHonorLinearLayout=(LinearLayout)findViewById(R.id.MemberSettingHonorLinearLayout);
		memberSettingLogoutLinearLayout=(LinearLayout)findViewById(R.id.MemberSettingLogoutLinearLayout);
		memberSettingRegisterSinaLinearLayout=(LinearLayout)findViewById(R.id.MemberSettingRegisterSinaLinearLayout);
		
		
		pushCheckBox=(CheckBox)findViewById(R.id.PushCheckBox);
		if(AppSharePreference.isAppSharePushStatus(this))
		{
			pushCheckBox.setChecked(true);
		}
		pushCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				DatabaseHelper dh=new DatabaseHelper(MemberSettingActivity.this);
				dh.insertUserAppPopularityBycatagory(MemberSettingActivity.this,2027);
				dh.close();
				if(pushCheckBox.isChecked())
				{
					AppSharePreference.setAppSharePushStatus(MemberSettingActivity.this,true);
				
				}else
				{
					AppSharePreference.setAppSharePushStatus(MemberSettingActivity.this,false);
				}
				Toast.makeText(MemberSettingActivity.this, "每日精品应用推送修改成功", Toast.LENGTH_SHORT).show();
			}
		});
		ImageView retBtn = (ImageView) findViewById(R.id.img_ret);
		retBtn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent=new Intent();
				intent.setClass(MemberSettingActivity.this, MainActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				finish();
			}
		});
		
		memberSettingModifyHeaderLinearLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				DatabaseHelper dh=new DatabaseHelper(MemberSettingActivity.this);
				dh.insertUserAppPopularityBycatagory(MemberSettingActivity.this,2026);
				dh.close();
				Intent	intent= new Intent();
				intent.setClass(MemberSettingActivity.this, MemberSettingHeaderActivity.class);
				intent.putExtra("registerFlag", false);
				startActivity(intent);
			}
		});
		memberSettingAccountLinearLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent	intent= new Intent();
				intent.setClass(MemberSettingActivity.this, AccountManagementActivity.class);
				startActivity(intent);
			}
		});
		memberSettingHonorLinearLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent	intent= new Intent();
				intent.setClass(MemberSettingActivity.this, MemberSettingHonorActivity.class);
				startActivity(intent);
			}
		});
		memberSettingLogoutLinearLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				DatabaseHelper dh = new DatabaseHelper(MemberSettingActivity.this);
				dh.insertUserAppPopularityBycatagory(MemberSettingActivity.this, 2031);
				dh.close();
				logoutBang();
			}
		});
		memberSettingRegisterSinaLinearLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				DatabaseHelper dh = new DatabaseHelper(MemberSettingActivity.this);
				dh.insertUserAppPopularityBycatagory(MemberSettingActivity.this, 2032);
				dh.close();
				smsRegister();
			}
		});
		
	}
	public void smsRegister()
	{
		RemoteApiImpl rai = new RemoteApiImpl() ;
		String registerNumber = rai.getSinaRegisterNumber(MemberSettingActivity.this);
		
		if(null==registerNumber || "".equals(registerNumber)){
			Log.e("MyTag", "Sim: error in getting SimCard");
			return;
		}
		
		Uri uri = Uri.parse("smsto:"+registerNumber);
	      Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
	      intent.putExtra("sms_body", "我正在使用安卓分享手机客户端玩新浪微博，方便又简单！");
	      startActivity(intent); 

	}

	public class DestroyBangAccountThread extends Thread 
	{
		private Context ctx;
		public DestroyBangAccountThread(Context ctx) 
		{
			this.ctx = ctx;
		}
		
		public void run() 
		{
			try 
			{
				LocalStore.getLocaleStoreInstance().logoutBangAccount();
				
			} catch(Exception ex) {
				ex.printStackTrace();
			}
		}
	}
}
